package edu.ccut.saturn.studio.menuitem.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;

public class RenameDialog extends Dialog {

	private Text text;
	private String name = ""; //$NON-NLS-1$
	private Button okbutton;

	/**
	 * Create the dialog
	 * 
	 * @param parentShell
	 */
	public RenameDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(500, 164);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("RenameDialog.renameshowname")); //$NON-NLS-1$
	}

	/**
	 * Create contents of the dialog
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 10;
		gridLayout.marginRight = 10;
		gridLayout.marginLeft = 10;
		gridLayout.marginBottom = 5;
		gridLayout.marginWidth = 3;
		gridLayout.marginTop = 20;
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.CENTER);
		label.setAlignment(SWT.CENTER);
		final GridData gd_label = new GridData(SWT.CENTER, SWT.CENTER, false,
				false);
		label.setLayoutData(gd_label);
		label.setText(Messages.getString("RenameDialog.showname")); //$NON-NLS-1$

		text = new Text(container, SWT.BORDER);
		text.setTextLimit(50);
		final GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_text.widthHint = 329;
		text.setLayoutData(gd_text);
		text.setText(getName());
		text.setSelection(getName().length());
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent modifyevent) {
				String fieldname = text.getText().trim();

				if (SaturnValidation.isEmpty(fieldname)
						|| SaturnValidation.isBadWord(fieldname)) {
					okbutton.setEnabled(false);
				} else {
					okbutton.setEnabled(true);
				}

				if (SaturnValidation.isEmpty(fieldname)) {
					MessageDialog
							.openInformation(
									null,
									(Messages.getString("RenameDialog.remend")), (Messages.getString("RenameDialog.shownamecannotempty"))); //$NON-NLS-1$ //$NON-NLS-2$
				}
				if (SaturnValidation.isBadWord(fieldname)) {
					MessageDialog
							.openInformation(
									null,
									(Messages.getString("RenameDialog.remend")), (Messages.getString("RenameDialog.nameillegal"))); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
		});

		return container;

	}

	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		okbutton = createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("RenameDialog.finish"), true); //$NON-NLS-1$
		okbutton.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("RenameDialog.cancel"), true); //$NON-NLS-1$
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			name = text.getText().trim();
		}

		super.buttonPressed(buttonId);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
